package com.example.snowliwork;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.snowliwork.fragment.BagFragment;
import com.example.snowliwork.fragment.FoodFragment;
import com.example.snowliwork.fragment.InfantFragment;
import com.example.snowliwork.fragment.RecommendFragment;
import com.example.snowliwork.fragment.StoreFragment;
import com.example.snowliwork.fragment.UnderwearFragment;
import com.example.snowliwork.fragment.WomenwearFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
//标签和视图
    //声明tab标签和ViewPager2
    private TabLayout tab1;
    private ViewPager2 vp2;

    //声明子页面的数据源，使用集合存储
    private List<Fragment> fragments;
    //声明子页面适配器
    private PageAdapter pageAdapter;


//视图内容的展示
    //gridview展示页面
    private GridView gvShop;
    //商品数据源
    private List<Shop> shops;
    //声明商品适配器对象
    private PlanetGridAdapter shopAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //获取控件对象
        getViews();


        //初始化子页面数据资源
        initPages();


        //初始化子页面适配器对象
        pageAdapter = new PageAdapter(fragments,this);
        //设置选项卡切换时滑动方向
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //给viewPage2绑定适配器
        vp2.setAdapter(pageAdapter);
        //关联ViewPager2和Tablayout
        TabLayoutMediator mediator = new TabLayoutMediator(
                tab1,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //设置选型卡文本
                        switch (position){
                            case 0:
                                tab.setText("推荐");
                                break;
                            case 1:
                                tab.setText("母婴");
                                break;
                            case 2:
                                tab.setText("百货");
                                break;
                            case 3:
                                tab.setText("鞋包");
                                break;
                            case 4:
                                tab.setText("食品");
                                break;
                            case 5:
                                tab.setText("内衣");
                                break;
                            case 6:
                                tab.setText("女装");
                                break;
                        }
                    }
                }

        );
        //使tablayout和viewpager2连接
        mediator.attach();



        //实例化planeGridAdapter对象
        shopAdapter = new PlanetGridAdapter(
                this,
                R.layout.infant_tablayout_activity,
                shops
        );
        //给用于显示商品信息的gridview绑定适配器
        gvShop.setAdapter(shopAdapter);





    }



    private void getViews() {
        //获取控件对象
        tab1 = findViewById(R.id.tab1);
        vp2 = findViewById(R.id.vp2);

        //获取GridView控件对象
        gvShop = findViewById(R.id.gv_plant);
//        LayoutInflater factorys = LayoutInflater.from(MainActivity.this);
//        View view = factorys.inflate(R.layout.recomment_tablayout_activity,null);
//        gvShop = (GridView) view.findViewById(R.id.gv_plant);



    }

    private void initPages() {
        //初始化集合对象
        fragments = new ArrayList<>();
        fragments.add(new RecommendFragment());
        fragments.add(new InfantFragment());
        fragments.add(new StoreFragment());
        fragments.add(new BagFragment());
        fragments.add(new FoodFragment());
        fragments.add(new UnderwearFragment());
        fragments.add(new WomenwearFragment());

        //初始化商品数据源
        shops = new ArrayList<>();
        //添加商品数据
        Shop fireworks = new Shop(
                R.drawable.fireworks,
                "包邮微瑕蜡烛花烛20厘米",
                "退货包运费 急速退款",
                "￥13"
        );
        shops.add(fireworks);
        Shop gril = new Shop(
                R.drawable.gril,
                "女童裤子 2022春新款",
                "退货包运费 满30反2",
                "￥14.88"
        );
        shops.add(gril);

        Shop mango = new Shop(
                R.drawable.mango,
                "【当天发出】正宗海南小台芒",
                "满21减3 坏了包赔",
                "￥18.7"
        );
        shops.add(mango);

        Shop lens = new Shop(
                R.drawable.lens,
                "一次性擦镜纸湿巾",
                "24小时发货",
                "￥1.7"
        );
        shops.add(lens);





    }

}
















